// pages/insureDeail/insureDeail.js
const util = require('../../utils/util.js')
import * as utils from '../../utils/util.js'
import request from '../../utils/http.js'
Page({
  data: {
    relationList: [],  //家庭关系列表
    InsuranceDetail: '',  //亲友团列表
    loginUser: false,//当前登陆人
    token: '',
    babyList: [],
    changeBabyData: {},
    ageStr: '',
    currentIndex: 0,
    headerImg: '',
    lock: true,
    goInsure: false
  },
  onLoad: function (options) {
    let that = this;
    //获取token数据
    let img = wx.getStorageSync("userInfo").avatar
    this.setData({
      token: wx.getStorageSync('token'),
      headerImg: img
    })
    //获取宝宝列表
    this.getBabyList();
  },
  onShow() {

  },
  //获取宝宝列表
  getBabyList() {
    utils.HttpRequst(true, "/baby-cloud-base/mobileBabyInfo/listAll", "post", {}, (res) => {
      console.log(res)
      if (res.data.code == 200 && res.data.data.length > 0) {
        this.setData({
          babyList: res.data.data,
          changeBabyData: res.data.data[0],
          ageStr: utils.getBirthSlot(res.data.data[0].birthday.split(" ")[0])
        })
        //默认请求第一个宝宝的亲友团列表   参数宝宝id
        this.getbridalList(this.data.babyList[0].id)
      }
    });
  },
  changeBabyHeader(e) {
    this.setData({
      currentIndex: e.target.dataset.idx,
      changeBabyData: this.data.babyList[e.target.dataset.idx],
      ageStr: utils.getBirthSlot(this.data.babyList[e.target.dataset.idx].birthday.split(" ")[0]),
      relationIndex: this.data.bridalList,//获取切换宝宝后的亲友团列表
    })
    this.getbridalList(this.data.changeBabyData.id)
  },
  //获取赠险详情
  getbridalList(babyId) {
    utils.HttpRequst(true, "/base/babyInsurance/detailByBabyId", "post", { babyId: babyId }, (res) => {
      if (res.data.code == 200) {
        this.setData({
          InsuranceDetail: res.data.data,
          goInsure: false
        })
      }else { 
        this.setData({
          goInsure: true
        })
      }
    })
  },

  //领取赠险
  goInsure: function () {
    let nowDay = new Date();
    let babyDay = new Date(this.data.changeBabyData.birthday.replace(/-/g, "/"));
    let days = nowDay.getTime() - babyDay.getTime();
    let day = parseInt(days / (1000 * 60 * 60 * 24)) + 1;
    console.log(day)
    wx.navigateTo({
      url: '/pages/receiveInsurance/receiveInsurance?dabyData=' + encodeURIComponent(JSON.stringify(this.data.changeBabyData)) + '&babybirthDays=' + day
    })
  },
})